﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MagicBag.CRM.Model;
using MagicBag.CRM.BLL;
using System.Web;

namespace MagicBag.CRM.Service
{
    public class DepartmentService : BaseService
    {
        /// <summary>
        /// 获取所有部门
        /// </summary>
        /// <param name="context"></param>
        public void GetDepartments(HttpContext context)
        {
            DepartmentBLL deptBLL = new DepartmentBLL();

            IList<Department> list = deptBLL.GetDepartments();// new List<CommonDictionary>();
            string s = string.Empty;
            if (context.Request["ddl"] == "1")//返回部门信息，提供给下拉列表使用
            {
                Department defualtDept = new Department();
                defualtDept.ID = 0;
                defualtDept.CName = "";
                list.Insert(0, defualtDept);
                s = JsonHelper.GetJson<IList<Department>>(list);
            }
            else
            {
                IList<Department> deptToJson = new List<Department>();

                foreach (Department dept in list)//设置每一个部门的直接子部门
                {
                    IList<Department> childrens = (from d in list where d.ParentID == dept.ID select d).ToList();
                    //IList<Department> childrens = deptBLL.GetChildrens(dept);
                    if (childrens != null && childrens.Count > 0)
                        dept.Childrens = childrens;
                    if(dept.ParentID==0)
                        deptToJson.Add(dept);//如果没有子结点就添加进去
                }
                s = GetEasyUIDetailJson<Department>(deptToJson, list.Count);
                s = s.Replace("Childrens", "children");//如果节点名字中出现了Childrens，将导致被替换，此为已知bug
            }
            ResponseJson(context, s);
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="context"></param>
        public void Insert(HttpContext context)
        {
            Department dept = new Department();
            int id;
            Int32.TryParse(context.Request["ID"], out id);
            dept.ID = id;
            dept.CName = context.Request["CName"];
            dept.EName = context.Request["EName"];
            dept.Remark = context.Request["Remark"];
            int pid;
            Int32.TryParse(context.Request["ParentID"], out pid);
            dept.ParentID = pid;

            Result result = new Result();
            result.Success = true;
            try
            {
                DepartmentBLL deptBLL = new DepartmentBLL();
                deptBLL.Insert(dept);
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.Msg = ex.Message;
            }
            ResponseJson(context, result.ToString());
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="context"></param>
        public void Delete(HttpContext context)
        {
            Result result = new Result();
            result.Success = true;

            int id;
            Int32.TryParse(context.Request["id"], out id);

            try
            {
                DepartmentBLL deptBLL = new DepartmentBLL();
                deptBLL.Delete(id);
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.Msg = ex.Message;
            }
            ResponseJson(context, result.ToString());
        }

        public void Update(HttpContext context)
        {
            Department dept = new Department();
            int id;
            Int32.TryParse(context.Request["ID"], out id);
            dept.ID = id;
            dept.CName = context.Request["CName"];
            dept.EName = context.Request["EName"];
            dept.Remark = context.Request["Remark"];
            int pid;
            Int32.TryParse(context.Request["ParentID"], out pid);
            dept.ParentID = pid;

            Result result = new Result();
            result.Success = true;
            try
            {
                DepartmentBLL deptBLL = new DepartmentBLL();
                deptBLL.Update(dept);
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.Msg = ex.Message;
            }
            ResponseJson(context, result.ToString());
        }
    }
}
